<?php

/**
 * This is the model class for table "ham_template".
 *
 * The followings are the available columns in table 'ham_template':
 * @property integer $id
 * @property string $name
 * @property string $description
 * @property string $content
 * @property integer $is_system
 * @property string $created_at
 * @property string $updated_at
 * @property integer $created_by
 *
 * The followings are the available model relations:
 * @property User $createdBy
 */
class Template extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Template the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ham_template';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required', 'message' => Yii::t('main', 'Vui lòng nhập tên mẫu email')),
			array('content', 'required', 'message' => Yii::t('main', 'Vui lòng nhập nội dung mẫu email')),
			array('name', 'length', 'max'=>200),
			array('description', 'length', 'max'=>255),
			// Please remove those attributes that should not be searched.
			array('id, name, description, content, is_system, created_at, updated_at, created_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'createdBy' => array(self::BELONGS_TO, 'User', 'created_by'),
		);
	}

	public function beforeSave(){
		if($this->isNewRecord){
			$this->created_at = date('Y-m-d H:i');
		}else{
			$this->updated_at = date('Y-m-d H:i');
		}
		return parent::beforeSave();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => Yii::t('main','Tên mẫu email'),
			'description' => Yii::t('main', 'Mô tả'),
			'content' => Yii::t('main','Nội dung'),
			'is_system' => 'Is System',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
			'created_by' => 'Created By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('is_system',$this->is_system);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);
		$criteria->compare('created_by',$this->created_by);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	// function to insert/update template
	public function updateTemplate(){
		$result = array('msg' => '', 'success' => false);
		if($this->isNewRecord){
			$this->created_by = Yii::app()->user->getId();
			$result['success'] = $this->insert();
			$result['msg'] = Yii::t('main', 'Tạo mới thành công!');
		}else{
			$result['success'] = $this->update();
			$result['msg'] = Yii::t('main', 'Cập nhật thành công!');
		}
		return $result;
	}
}